package net.messagenet.mazor.infra.excel;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import jxl.read.biff.BiffException;
import jxl.write.WriteException;

import net.messagenet.mazor.DO.DpaDO;
import net.messagenet.mazor.DO.PosDO;
import net.messagenet.mazor.exception.MZRInternalServerException;
import net.messagenet.mazor.infra.InfrastructureConstants;

import org.apache.log4j.Logger;

/**
 * This class is the standard implementation of the mazor system excel manager
 * it write/read excel pages using JXL jars
 * 
 * @author Idan
 *
 */

public class ExcelManagerImpl implements ExcelManager {
	
	
	
	private Logger logger =  Logger.getLogger(this.getClass());
	
	private String path;
	private Properties excelConstats ;
	
	private DpaRequestExcelHandler dpaWriter ;

	private PosRequestExcelHandler posWriter;
	
	public ExcelManagerImpl() throws IOException {
		logger.debug("loading properties file");
		excelConstats = new Properties();
		excelConstats.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(InfrastructureConstants.EXCEL_FILE_CONSTATNS));
		dpaWriter = new DpaRequestExcelHandler(excelConstats);
		posWriter = new PosRequestExcelHandler(excelConstats);
		logger.info("Excel Manager built ");
	}
	
	@Override
	public String createDPARequestSheet(DpaDO dpa,String fileName) throws MZRInternalServerException{
		
		String fullPath= path+fileName+".xls";
		try {
			logger.debug("writting excel file for the DPA request");
			dpaWriter.writeDPAExcelFile(dpa,fullPath);
		} catch (IOException e) {
				throw new MZRInternalServerException("IOException", e);
		} catch (WriteException e) {
			throw new MZRInternalServerException("WriteException ", e);
		}
		return fullPath;
	}


	/**
	 * 
	 * 
	 * @return the folder to which the sheets will be written to
	 */
	public String getPath() {
		return path;
	}

	/**
	 * sets a new path to which the sheets will be written to
	 * @param path
	 * @throws MZRInternalServerException when the path is incorrect
	 */
	public void setPath(String path) throws MZRInternalServerException {
		File fPath = new File(path);
		if (fPath.exists()&& fPath.isDirectory() ){
			logger.info("Excel files output folder is set to: "+path);
			this.path = path; 
		}else{
			throw new MZRInternalServerException("The output path does not exists");
		}
	}

	@Override
	public DpaExcelData readDPADataFromFile(String file) throws BiffException,
			IOException {
		return dpaWriter.readDPADataFromFile(file);
	}

	@Override
	public String createPOSRequestSheet(PosDO posDo,String fileName) throws MZRInternalServerException {
		String fullPath= path+fileName+".xls";
		try {
			logger.debug("writting excel file for the DPA request");
			posWriter.writePOSExcelFile(posDo,fullPath);
		} catch (IOException e) {
				throw new MZRInternalServerException("IOException", e);
		} catch (WriteException e) {
			throw new MZRInternalServerException("WriteException ", e);
		}
		return fullPath;
	}
}
